package com.yang.qgg_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.qgg_demo.common.R;
import com.yang.qgg_demo.entity.Bill;
import com.yang.qgg_demo.entity.Goods;
import com.yang.qgg_demo.entity.User;
import com.yang.qgg_demo.service.BillService;
import com.yang.qgg_demo.service.IUserService;
import com.yang.qgg_demo.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理
 */
@Slf4j
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private IUserService userService;

    /**
     * 新增商品
     * @param goods
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Goods goods){

        log.info(goods.toString());

        Bill bill = new Bill();
        //bill.setId(2);
        String currentUser =  TokenUtils.getUser();
        int id = Integer.parseInt(currentUser);
        if(id != 0){
            bill.setUserid(id);
        }else bill.setUserid(1);
        //bill.setCategoryId(goods.getCategoryId());
        bill.setMoney(goods.getPrice());

        billService.save(bill);

        return R.success(String.valueOf(bill));
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Page<Bill> findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize) {

        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();

        Page pageInfo=new Page(pageNum, pageSize);
        String currentUser =  TokenUtils.getUser();
        int id = Integer.parseInt(currentUser);


        if (id>0&&userService.getById(id).getRoleid()!=1){
            queryWrapper.eq("userid",id);
            return billService.page(pageInfo,queryWrapper);
        }


        return billService.page(new Page<>(pageNum,pageSize),queryWrapper);

    }

}
